package net.jjjshop.front.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.constants.ConfConstants;
import net.jjjshop.common.entity.lbt.LbSysConfigEntity;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.service.lbt.LbLotteryOrderService;
import net.jjjshop.common.service.lbt.LbSysConfigService;
import net.jjjshop.common.service.lbt.UserBillService;
import net.jjjshop.common.service.user.UserService;
import net.jjjshop.common.util.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Component
@EnableScheduling // 2.开启定时任务
public class SettlementScheduled {
   //每月1号凌晨1点结算
   @Autowired
   private UserService userService;
    @Autowired
    private UserBillService userBillService;

    @Autowired
    private LbLotteryOrderService lotteryOrderService;
    @Autowired
    private LbSysConfigService sysConfigService;
//    @Scheduled(cron = "0 59 1 * * ?")
    @Scheduled(cron = "0 0 1 1 * ?")
    public void init(){
        LbSysConfigEntity lbSysConfigEntity = sysConfigService.selectSysConfig();
        if (lbSysConfigEntity.getPool1().doubleValue() >0){
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getLevel4,4);
            wrapper.eq(User::getLevelControl,1);
            List<User> users = userService.list(wrapper);
            if (null != users && !users.isEmpty()){
            //算出平均人数收益
        BigDecimal bigDecimal = BigDecimalUtil.divide(lbSysConfigEntity.getPool1(),new BigDecimal(users.size()));
//
//        BigDecimal bigDecimal = BigDecimalUtil.divide(bsa,new BigDecimal(users.size()));
        for (User user : users){
            userBillService.saveBill("总代月度分红", ConfConstants.COMMISSION_TYPE,1, bigDecimal, user.getUserId(), ConfConstants.YJ_FLAG,null);

          }
                lbSysConfigEntity.setPool1(new BigDecimal(0));
            }
        }

        if (lbSysConfigEntity.getPool2().doubleValue() >0){
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getLevel4,5);
            wrapper.eq(User::getLevelControl,1);
            List<User> users = userService.list(wrapper);
            if (null != users && !users.isEmpty()){
                //算出平均人数收益
                BigDecimal bigDecimal = BigDecimalUtil.divide(lbSysConfigEntity.getPool2(),new BigDecimal(users.size()));
//
//        BigDecimal bigDecimal = BigDecimalUtil.divide(bsa,new BigDecimal(users.size()));
                for (User user : users){
                    userBillService.saveBill("联创月度分红", ConfConstants.COMMISSION_TYPE,1, bigDecimal, user.getUserId(), ConfConstants.YJ_FLAG,null);

                }
                lbSysConfigEntity.setPool2(new BigDecimal(0));
            }
        }

        sysConfigService.updateById(lbSysConfigEntity);

    }
    @Scheduled(cron = "0 5 11 * * ?")
    public void lottery(){
//        lotteryOrderService.getQueryLottery();
    }
}
